package com.thssh.ui.lsn03.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

/**
 * @author zhangyugehu
 * @version V1.0
 * @date 2017/09/11
 * email: zhangyugehu@163.com
 */

public class PaintTextView extends View {

    private Paint mTextPaint;
    private Paint mLinePaint;

    public PaintTextView(Context context) {
        this(context, null);
    }

    public PaintTextView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public PaintTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mLinePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec), 400);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        mTextPaint.setTextSize(200);
        mTextPaint.setAlpha(255 / 3);

//        mLinePaint.setStrokeWidth(5);
        mLinePaint.setTextSize(20);


        String text = "北京Beijing";
        int startX = 20;
        int endX = startX + (int) mTextPaint.measureText(text);

        int baseLineY = 300;

        Paint.FontMetricsInt fmi = mTextPaint.getFontMetricsInt();
        int ascentY = fmi.ascent + baseLineY;
        int bottomY = fmi.bottom + baseLineY;
        int descentY = fmi.descent + baseLineY;
        int leadingY = fmi.leading + baseLineY;
        int topY = fmi.top + baseLineY;
        int centerY = (topY + bottomY) / 2;

//        Log.d("PaintTextView", "topY: " + (topY + baseLineY));
//        Log.d("PaintTextView", "leadingY: " + (leadingY + baseLineY));
//        Log.d("PaintTextView", "descentY: " + (descentY + baseLineY));
//        Log.d("PaintTextView", "bottomY: " + (bottomY + baseLineY));
//        Log.d("PaintTextView", "ascentY: " + (ascentY + baseLineY));
//        Log.d("PaintTextView", "baseLineY: " + (0 + baseLineY));

        canvas.drawText(text, startX, baseLineY, mTextPaint);

        // top
        mLinePaint.setColor(Color.RED);
        canvas.drawText("top", startX, topY - 5, mLinePaint);
        canvas.drawLine(startX, topY, endX, topY, mLinePaint);

        // ascent
        mLinePaint.setColor(Color.BLUE);
        canvas.drawText("ascent", startX + 200, ascentY - 5, mLinePaint);
        canvas.drawLine(startX, ascentY, endX, ascentY, mLinePaint);

        // center
        mLinePaint.setColor(Color.RED);
        canvas.drawText("center", startX, centerY - 5, mLinePaint);
        canvas.drawLine(startX, centerY, endX, centerY, mLinePaint);

        // leading
        mLinePaint.setColor(Color.BLUE);
        canvas.drawText("leading", startX + 200, leadingY - 5, mLinePaint);
        canvas.drawLine(startX, leadingY, endX, leadingY, mLinePaint);

        // baseLine
        mLinePaint.setColor(Color.RED);
        canvas.drawText("baseLine", startX, baseLineY - 5, mLinePaint);
        canvas.drawLine(startX, baseLineY, endX, baseLineY, mLinePaint);

        // descent
        mLinePaint.setColor(Color.BLUE);
        canvas.drawText("descent", startX + 200, descentY - 5, mLinePaint);
        canvas.drawLine(startX, descentY, endX, descentY, mLinePaint);

        // bottom
        mLinePaint.setColor(Color.RED);
        canvas.drawText("bottom", startX, bottomY - 5, mLinePaint);
        canvas.drawLine(startX, bottomY, endX, bottomY, mLinePaint);
    }
}
